package com.cuixichang.minimalism.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cuixichang.minimalism.basal.base.Result;
import com.cuixichang.minimalism.basal.cnstants.HttpHeader;
import com.cuixichang.minimalism.basal.core.RelationDict;
import com.cuixichang.minimalism.basal.enums.DefaultNumberEnum;
import com.cuixichang.minimalism.basal.enums.ResponseCoderEnum;
import com.cuixichang.minimalism.core.security.userdetails.CredentialDetail;
import com.cuixichang.minimalism.core.security.utils.SecurityBeanUtil;
import com.cuixichang.minimalism.domain.ISystemCommandService;
import com.cuixichang.minimalism.transfer.dto.ISystemDTO;
import com.cuixichang.minimalism.transfer.factory.ISystemAssembler;
import com.cuixichang.minimalism.transfer.page.ISystemPageRequest;
import com.cuixichang.minimalism.transfer.page.ISystemRequest;
import com.cuixichang.minimalism.transfer.po.ISystem;
import com.cuixichang.minimalism.transfer.vo.ISystemVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import com.cuixichang.minimalism.service.ISystemService;
import org.springframework.beans.factory.annotation.Autowired;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 系统信息注册表表控制层
 *
 * @author cuixichang
 * @date 2024-11-22
 */
@Api(tags = "系统信息注册表对象功能接口")
@RestController
@RequestMapping("system")
@RequiredArgsConstructor
public class ISystemController {
    @Autowired
    private ISystemService iSystemService;
    @Autowired
    private ISystemCommandService iSystemCommandService;
    @PostMapping("/info")
    @ApiOperation(value = "创建系统信息")
    public Result<ISystemVO> insert(@RequestBody ISystemRequest request){
        ISystemDTO convert = ISystemAssembler.INSTANCT.convertDto(request);
        CredentialDetail credentialDetail = SecurityBeanUtil.getCredentialDetail();
        convert.setCreateLoginId(Objects.nonNull(credentialDetail)?credentialDetail.getLoginId(): DefaultNumberEnum.Zero.getCode());
        ISystemDTO iSystem = iSystemService.insert(convert);
        return Result.ok("创建系统信息完成", ISystemAssembler.INSTANCT.convertVo(iSystem));
    }

    @DeleteMapping("/info/{id}")
    @ApiOperation(value = "删除系统信息通过ID")
    public Result deleteById(@PathVariable Long id){
        boolean deleteFlag = iSystemService.deleteById(id) == 1;
        return deleteFlag?Result.ok(ResponseCoderEnum.Success.getDesc(), true):
                Result.error(ResponseCoderEnum.Failed.getCode(), "删除失败");
    }

    @PutMapping("/info/{id}")
    @ApiOperation(value = "修改系统信息通过ID")
    public Result updateById(@PathVariable Long id, @RequestBody ISystemRequest request){
        ISystemDTO systemDTO = ISystemAssembler.INSTANCT.convertDto(request);
        systemDTO.setId(id);
        CredentialDetail credentialDetail = SecurityBeanUtil.getCredentialDetail();
        systemDTO.setCreateLoginId(Objects.nonNull(credentialDetail)?credentialDetail.getLoginId(): DefaultNumberEnum.Zero.getCode());
        boolean update = iSystemService.updateById(systemDTO) == 1;
        return update?Result.ok(ResponseCoderEnum.Success.getDesc(), true):
                Result.error(ResponseCoderEnum.Failed.getCode(), "修改失败");
    }

    @GetMapping("/info/{id}")
    @ApiOperation(value = "查询系统信息通过ID")
    public Result<ISystemVO> selectById(@PathVariable("id") Long id){
        return Result.ok("查询系统信息完成", BeanUtil.toBean(iSystemService.selectById(id), ISystemVO.class));
    }

    @GetMapping("/infoByCode/{systemCode}")
    @ApiOperation(value = "查询系统信息通过SystemCode")
    public Result<ISystemVO> selectByCode(@PathVariable String systemCode){
        ISystemDTO system = iSystemService.selectOne(ISystemDTO.builder().systemCode(systemCode).build());
        return Result.ok("查询系统信息完成", BeanUtil.toBean(system, ISystemVO.class));
    }

    @GetMapping("/page")
    @ApiOperation(value = "分页条件查询系统信息")
    public Result<Page<ISystemVO>> selectPageByConditional(ISystemPageRequest request){
        ISystemDTO systemDTO = SecurityBeanUtil.request2dto(request, ISystemDTO.class);
        Page<ISystemDTO> iSystemPage = iSystemCommandService.selectPageByConditional(systemDTO, request.getPageNo(), request.getPageSize());
        Page<ISystemVO> iSystemVOPage = new Page<>(iSystemPage.getCurrent(), iSystemPage.getSize(), iSystemPage.getTotal());
        iSystemVOPage.setRecords(iSystemPage.getRecords().stream().map(po -> BeanUtil.toBean(po, ISystemVO.class)).collect(Collectors.toList()));
        return Result.ok("分页查询用户角色信息完成",iSystemVOPage);
    }

    @GetMapping("/params")
    @ApiOperation(value = "查询可操作系统编码列表")
    public Result<List<RelationDict>> selectQuerySystemParams(@RequestHeader(HttpHeader.SYSTEM_SOURCE_CODE) @ApiIgnore String systemCode){
        CredentialDetail credentialDetail = SecurityBeanUtil.getCredentialDetail();
        List<RelationDict> iSystemList = iSystemCommandService.selectQuerySystemParams(systemCode,credentialDetail);
        return Result.ok("查询系统信息列表完成",iSystemList);
    }
}
